package cn.csc.module.member.controller.app.address;

import cn.csc.framework.security.core.annotations.PreAuthenticated;
import cn.csc.module.member.controller.app.address.vo.AppMemberAddressCreateReqVO;
import cn.csc.module.member.controller.app.address.vo.AppMemberAddressRespVO;
import cn.csc.module.member.controller.app.address.vo.AppMemberAddressUpdateReqVO;
import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.module.member.convert.address.MemberAddressConvert;
import cn.csc.module.member.dal.dataobject.address.MemberAddressDO;
import cn.csc.module.member.service.address.MemberAddressService;
import cn.hutool.core.collection.CollUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户收件地址")
@RestController
@RequestMapping("/member/address")
@Validated
@Slf4j
public class AppMemberAddressController {

    @Resource
    private MemberAddressService memberAddressService;

    @PostMapping("/create")
    @Operation(summary = "创建用户收件地址")
    public CommonResult<Long> createAddress(@Valid @RequestBody AppMemberAddressCreateReqVO createReqVO) {
        return success(memberAddressService.createAddress(getLoginUserId(), createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户收件地址")
    public CommonResult<Boolean> updateAddress(@Valid @RequestBody AppMemberAddressUpdateReqVO updateReqVO) {
        memberAddressService.updateAddress(getLoginUserId(), updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户收件地址")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteAddress(@RequestParam("id") Long id) {
        memberAddressService.deleteAddress(getLoginUserId(), id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户收件地址")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppMemberAddressRespVO> getAddress(@RequestParam("id") Long id) {
        MemberAddressDO address = memberAddressService.getAddress(getLoginUserId(), id);
        return success(MemberAddressConvert.INSTANCE.convert(address));
    }

    @GetMapping("/get-default")
    @Operation(summary = "获得默认的用户收件地址")
    @PermitAll
    public CommonResult<AppMemberAddressRespVO> getDefaultUserAddress() {
        Long userId=getLoginUserId();//v2调整不需要登录也能访问
        log.info("[getDefaultUserAddress][userId]:{}",userId);
        if(userId == null){
            return success();
        }
        MemberAddressDO address = memberAddressService.getDefaultUserAddress(userId);
        if(address == null){//默认地址没有返回，获取一条用户地址
            List<MemberAddressDO> list = memberAddressService.getAddressList(userId);
            address = CollUtil.isNotEmpty(list) ? list.get(0) : new MemberAddressDO();
        }
        return success(MemberAddressConvert.INSTANCE.convert(address));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户收件地址列表")
    public CommonResult<List<AppMemberAddressRespVO>> getAddressList() {
        List<MemberAddressDO> list = memberAddressService.getAddressList(getLoginUserId());
        return success(MemberAddressConvert.INSTANCE.convertList(list));
    }

}
